package com.example.astronautcontact2206044090cxt1104;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class ContactDetailActivity extends AppCompatActivity {
    private ConstraintLayout main;
    private TextView tvContactDetailName;
    private TextView tvContactDetailPhone;
    private TextView tvContactDetailOrganization;
    private TextView tvContactDetailEmail;
    private TextView tvContactDetailNotes;
    private ImageView ivContactDetailCall;
    private ImageView ivContactDetailMark;
    private ImageView ivContactDetailEdit;
    private ImageView ivContactDetailMore;
    private ContactDAL contactDAL=new ContactDAL(this);
    private AstronautContact contact;
    private int id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_contact_detail);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        initView();
        id=getIntent().getIntExtra("id",-1);
        if(id!=-1){
            contact=contactDAL.queryById(id);
            tvContactDetailName.setText(contact.getName());
            tvContactDetailPhone.setText(contact.getPhone());
            tvContactDetailOrganization.setText(contact.getOrganization()
            );
            tvContactDetailEmail.setText(contact.getEmail());
            tvContactDetailNotes.setText(contact.getNotes());
        }
        ivContactDetailCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:" + contact.getPhone()));
                startActivity(intent);
            }
        });
        ivContactDetailEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = getLayoutInflater().inflate(R.layout.dialog_contact, null);
                ((EditText)view.findViewById(R.id.edtDialogName)).setText(contact.getName());
                ((EditText)view.findViewById(R.id.edtDialogOrganization)).setText(contact.getOrganization());
                ((EditText)view.findViewById(R.id.edtDialogPhone)).setText(contact.getPhone());
                ((EditText)view.findViewById(R.id.edtDialogNotes)).setText(contact.getNotes());
                ((EditText)view.findViewById(R.id.edtDialogEmail)).setText(contact.getEmail());
                new AlertDialog.Builder(ContactDetailActivity.this)
                    .setTitle("编辑联系人")
                    .setView(view)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String name=((EditText)view.findViewById
                                (R.id.edtDialogName)).getText().toString().trim();
                        String organization=((EditText)view.findViewById
                                (R.id.edtDialogOrganization)).getText().toString().trim();
                        String phone=((EditText)view.findViewById
                                (R.id.edtDialogPhone
                        )).getText().toString().trim();
                        String notes=((EditText)view.findViewById
                                (R.id.edtDialogNotes
                        )).getText().toString().trim();
                        String email=((EditText)view.findViewById(R.id.edtDialogEmail
                        )).getText().toString().trim();
                        AstronautContact contactUpdate=new AstronautContact
                                (id,name,phone,email,organization,notes);
                        if(contactDAL.update(contactUpdate)!=-1){
                            contact = contactDAL.queryById(id);
                            tvContactDetailName.setText(contact.getName());
                            tvContactDetailPhone.setText(contact.getPhone());
                            tvContactDetailOrganization.setText(contact.getOrganization());
                            tvContactDetailEmail.setText(contact.getEmail());
                            tvContactDetailNotes.setText(contact.getNotes());
                            Toast.makeText(ContactDetailActivity.this,
                                    "更新成功", Toast.LENGTH_SHORT).show();
                            }else {
                            Toast.makeText(ContactDetailActivity.this,
                                    "更新失败", Toast.LENGTH_SHORT).show();
                            }
//                        contactDAL.delete(contact.getId());
//                        startActivity(new Intent(ContactDetailActivity.this,
//                                MainActivity.class));
//                        finish();
                    }
                })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                    .create()
                    .show();
            }
        });
        ivContactDetailMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popupMenu = new PopupMenu(ContactDetailActivity.this,v);
                popupMenu.getMenuInflater().inflate(R.menu.contact_popup_menu, popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        int itemID = menuItem.getItemId();
                        if (itemID == R.id.itemMenuDel) {
                             //删除联系人
                            new AlertDialog.Builder(ContactDetailActivity.this)
                            .setMessage("确定删除该联系人？")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                                 @Override
                                 public void onClick(DialogInterface dialog, int which) {
                                     contactDAL.delete(contact.getId());
                                     startActivity(new Intent(
                                     ContactDetailActivity.this, MainActivity.class));
                                     finish();
                                        }
                                    })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                 @Override
                                 public void onClick(DialogInterface dialog, int which) {
                                       dialog.dismiss();
                                        }
                                 })
                            .create()
                            .show();
                            return true;
                             } else if (itemID == R.id.itemMenuAdd) {
                             //添加到手机通讯录
                             return true;
                             } else if (itemID == R.id.itemMenuMark) {
                             //联系人备忘录
                             return true;
                             } else {
                             return false;
                             }
                    }
                });
                popupMenu.show();
            }
        });
    }

    private void initView() {
        main =(ConstraintLayout) findViewById(R.id.main);
        tvContactDetailName=(TextView) findViewById(R.id.tvContactDetailName);
        tvContactDetailPhone = (TextView) findViewById(R.id.tvContactDetailPhone);
        tvContactDetailOrganization = (TextView) findViewById(R.id.tvContactDetailOrganization);
        tvContactDetailEmail = (TextView) findViewById(R.id.tvContactDetailEmail);
        tvContactDetailNotes = (TextView) findViewById(R.id.tvContactDetailNotes);
        ivContactDetailCall = (ImageView) findViewById(R.id.ivContactDetailCall);
        ivContactDetailMark = (ImageView) findViewById(R.id.ivContactDetailMark);
        ivContactDetailEdit = (ImageView) findViewById(R.id.ivContactDetailEdit);
        ivContactDetailMore = (ImageView) findViewById(R.id.ivContactDetailMore);
    }

}